package hottargui.standard;

import java.util.Map;

import hottargui.framework.Game;
import hottargui.framework.GameData;
import hottargui.framework.IllegalActionException;
import hottargui.framework.Player;
import hottargui.framework.PlayerColor;
import hottargui.framework.Position;
import hottargui.framework.State;
import hottargui.framework.TarguiState;
import hottargui.standard.StandardPlayer;

public class StandardNewGameState implements TarguiState {
	
	Game gameContext;
	
	public StandardNewGameState(Game game)
	{
		this.gameContext = game;
	}
	
	public boolean buy(int count, Position deploy) throws IllegalActionException {
		throw new IllegalActionException(this.getState());
	}

	public State getState() {
		return State.newGame;
	}

	public boolean move(Position from, Position to, int count) throws IllegalActionException {
		throw new IllegalActionException(this.getState());
	}

	public void newGame() throws IllegalActionException {
		GameData gd = gameContext.getGameObjectServer().getGameData();
		
		// Setup board
		gameContext.getGameObjectServer().getBoardStrategy().setupBoard(gd);
		
		Map<PlayerColor, Player> players = gd.getPlayers();

		// setup players
		Player redPlayer = new StandardPlayer(PlayerColor.Red);
		((StandardPlayer)redPlayer).setSettlement(new Position(0, 0));
		players.put(PlayerColor.Red, redPlayer);
		
		Player greenPlayer = new StandardPlayer(PlayerColor.Green);
		((StandardPlayer)greenPlayer).setSettlement(new Position(0, 6));
		players.put(PlayerColor.Green, greenPlayer);
		
		Player bluePlayer = new StandardPlayer(PlayerColor.Blue);
		((StandardPlayer)bluePlayer).setSettlement(new Position(6, 0));
		players.put(PlayerColor.Blue, bluePlayer);
		
		Player yellowPlayer = new StandardPlayer(PlayerColor.Yellow);
		((StandardPlayer)yellowPlayer).setSettlement(new Position(6, 6));
		players.put(PlayerColor.Yellow, yellowPlayer);
		
		// setup rounds left (25 - the one about to take place)
		gd.setRoundsLeft(24);
	}

	public void rollDie() throws IllegalActionException {
		throw new IllegalActionException(this.getState());
	}

	public PlayerColor turnCard() throws IllegalActionException {
		gameContext.setState(gameContext.getFactory().getMoveState(gameContext));
		
		PlayerColor nextPlayer = gameContext.getGameObjectServer().getTurnSequenceStrategy().nextPlayer();
		return gameContext.getGameObjectServer().getGameData().setPlayerInTurn(nextPlayer);
	}

	public PlayerColor getWinner() throws IllegalActionException {
		throw new IllegalActionException(this.getState());
	}

}
